package cn.yangliu.springbatch.job;

import cn.yangliu.springbatch.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;

/**
 * the type of springbatch
 *
 * <p>
 * .
 *
 * @author 杨柳
 * @date 2021-07-12
 */
@Component
public class UserProcessor implements ItemProcessor<User, User> {

    /**
     * 日志记录操作对象 slf4j门面框架
     * 定义为常量
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(UserProcessor.class);

    private StepExecution stepExecution;

    @BeforeStep
    public void beforeStep(StepExecution stepExecution) {
        this.stepExecution = stepExecution;
    }

    @Override
    public User process(User user) throws Exception {
        Object user1 = stepExecution.getExecutionContext().get("user");
        LOGGER.info("step context中的数据 : " + user1);
        //模拟  假装处理数据,这里处理就是打印一下

        LOGGER.info("processor data : " + user.toString());
        user.setAge(user.getAge() + 1);
        return user;
    }
}
